package com.whfc.wxmp.api.safety.service.impl;

import com.whfc.common.exception.BizException;
import com.whfc.common.file.FileHandler;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.result.PageVO;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.AssertUtil;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.RandomUtil;
import com.whfc.entity.dto.statistics.CheckDetailDTO;
import com.whfc.entity.dto.statistics.CheckStatisticsDTO;
import com.whfc.entity.param.check.CheckSearchParam;
import com.whfc.fuum.dto.SysUserDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.entity.WxUser;
import com.whfc.fuum.service.SysUserService;
import com.whfc.safety.dto.SafetyCheckDescDTO;
import com.whfc.safety.dto.SafetyCheckListDTO;
import com.whfc.safety.dto.SafetyCheckStatisticalDTO;
import com.whfc.safety.enums.SafetyCheckUserType;
import com.whfc.safety.param.SafetyCheckListParam;
import com.whfc.safety.param.SafetyCheckLogParam;
import com.whfc.safety.param.SafetyCheckParam;
import com.whfc.safety.param.SafetyCheckUserParam;
import com.whfc.safety.service.SafetyCheckService;
import com.whfc.wxmp.api.safety.service.WxSafetyCheckService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @ClasssName WxSafetyCheckServiceImpl
 * @Description 质量问题接口实现
 * @Author hw
 * @Date 2020/7/30 11:12
 * @Version 1.0
 */
@Service
public class WxSafetyCheckServiceImpl implements WxSafetyCheckService {

    @DubboReference(interfaceClass = SafetyCheckService.class, version = "1.0.0")
    private SafetyCheckService safetyCheckService;

    @DubboReference(interfaceClass = SysUserService.class, version = "1.0.0")
    private SysUserService sysUserService;

    @Autowired
    private FileHandler fileHandler;

    @Autowired
    private FilePathConfig filePathConfig;

    @Override
    public SafetyCheckListDTO getCheckList(Integer deptId, Integer[] type, Integer urgency, Integer overdue,
                                           Integer pageNum, Integer pageSize, String phone, Integer[] state, Date startTime, Date endTime) {
        SysUser user = sysUserService.getUserByPhone(phone);
        if (user == null) {
            throw new BizException(ResultEnum.NOT_FOUND.getCode(), "请联系管理员添加账号");
        }
        SafetyCheckListParam checkListParam = new SafetyCheckListParam();
        checkListParam.setDeptId(deptId);
        checkListParam.setType(type);
        checkListParam.setState(state);
        checkListParam.setStartTime(startTime);
        checkListParam.setEndTime(endTime);
        if (pageNum != null && pageSize != null) {
            checkListParam.setPageNum(pageNum);
            checkListParam.setPageSize(pageSize);
        }
        checkListParam.setUserId(user.getId());
        checkListParam.setUrgency(urgency);
        checkListParam.setOverdue(overdue);
        return safetyCheckService.list(checkListParam);
    }

    @Override
    public SafetyCheckDescDTO getCheck(Integer checkId, WxUser user) {
        return safetyCheckService.desc(checkId);
    }

    @Override
    public void add(SafetyCheckParam checkParam, SysUser sysUser) {
        SafetyCheckUserParam userParam = new SafetyCheckUserParam();
        userParam.setType(SafetyCheckUserType.REPORTER.getValue());
        userParam.setUserId(sysUser.getId());
        userParam.setUserName(sysUser.getNickname());
        userParam.setUserPhone(sysUser.getPhone());
        checkParam.getCheckUsers().add(userParam);
        Collections.sort(checkParam.getCheckUsers());

        checkParam.setCheckUserId(sysUser.getId());
        checkParam.setCheckUserName(sysUser.getNickname());
        safetyCheckService.addCheck(checkParam);
    }

    @Override
    public void addCheckLog(SafetyCheckLogParam safetyCheckLogParam, String userPhone) {
        SysUser sysUser = sysUserService.getUserByPhone(userPhone);
        if (sysUser == null) {
            throw new BizException(ResultEnum.NOT_FOUND.getCode(), "请联系管理员添加账号");
        }
        safetyCheckLogParam.setOpUserId(sysUser.getId());
        safetyCheckLogParam.setOpUserName(sysUser.getNickname());
        //发送消息
        safetyCheckService.addCheckLog(safetyCheckLogParam);
    }

    @Override
    public SafetyCheckStatisticalDTO statistical(Integer deptId, Date time) {
        Date startTime = DateUtil.getMonthBegin(time);
        Date endTime = DateUtil.getMonthEnd(time);
        return safetyCheckService.statistical(deptId, startTime, endTime, 1);
    }

    @Override
    public List<SysUserDTO> sysUserList(Integer deptId,String keyword) {
        List<SysUserDTO> res = sysUserService.getUsersByDeptId(deptId);
        if (CollectionUtils.isEmpty(res) || StringUtils.isBlank(keyword)) {
            return res;
        }
        return res.stream().filter(v -> (v.getNickname() != null && v.getNickname().contains(keyword)) || (v.getUsername() != null && v.getUsername().contains(keyword))).collect(Collectors.toList());
    }

    @Override
    public String uploadImg(MultipartFile file) {
        try {
            String fileName = RandomUtil.getRandomFileName() + ".png";
            InputStream inputStream = file.getInputStream();
            String ossKey = filePathConfig.getFilePath("safety/img", fileName);
            return fileHandler.upload(ossKey, inputStream);
        } catch (IOException e) {
            return null;
        }
    }


    @Override
    public SafetyCheckListDTO getCheckListByDeptId(Integer deptId) {

        SafetyCheckListParam checkListParam = new SafetyCheckListParam();
        checkListParam.setDeptId(deptId);
        return safetyCheckService.list(checkListParam);
    }

    @Override
    public CheckStatisticsDTO getCheckStatistics(CheckSearchParam param) {
        return safetyCheckService.getCheckStatistics(param);
    }

    @Override
    public PageVO<CheckDetailDTO> getCheckPage(CheckSearchParam param) {
        return safetyCheckService.getCheckPage(param);
    }
}
